package com.powernobug.servlet;

import com.powernobug.bean.model.MarketIssue;
import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.PageVo;
import com.powernobug.bean.vo.data.IssueCreateData;
import com.powernobug.bean.vo.param.IssueCreateVo;
import com.powernobug.bean.vo.param.IssueListParam;
import com.powernobug.service.IssueService;
import com.powernobug.service.impl.IssueServiceImpl;
import com.powernobug.util.JacksonUtil;
import com.powernobug.util.JsonUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Map;

/**
 *
 */
/*

 */
@WebServlet("/issue/*")
public class AdminIssueServlet extends CommonServlet {
    IssueService issueService = new IssueServiceImpl();

    /*
    http://39.101.189.16:8083/admin/issue/list?page=1&limit=20&sort=add_time&order=desc
    请求方法:
    GET
     */
    public BaseRespVo list(HttpServletRequest request) throws IOException, ParseException {
        // 1、接收请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        IssueListParam issueListParam = new IssueListParam();
        try {
            BeanUtils.copyProperties(issueListParam, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        // 2、 Model
        PageVo data = issueService.list(issueListParam);

        // 3、响应结果
        BaseRespVo baseRespVo = BaseRespVo.ok(data);
        return baseRespVo;
    }

    public BaseRespVo create(HttpServletRequest request) throws IOException {
        // 1、请求参数
        String jsonStr = request.getReader().readLine();
        IssueCreateVo issueCreateVo = JacksonUtil.read(jsonStr, IssueCreateVo.class);

        // 2、Service处理业务
        IssueCreateData data = issueService.create(issueCreateVo);

        // 3、响应结果
        return BaseRespVo.ok(data);
    }

    public BaseRespVo update(HttpServletRequest request) throws IOException {
        // 1、请求参数
        MarketIssue marketIssue1 = JsonUtil.readJson(request.getReader().readLine(), MarketIssue.class);
//        String jsonStr = request.getReader().readLine();
////        MarketIssue marketIssue = JacksonUtil.read(jsonStr, MarketIssue.class);
//        Map updateInfoMap = JacksonUtil.read(jsonStr, Map.class);

        // 2、Service处理业务
//        issueService.update(marketIssue);
        MarketIssue marketIssue = issueService.updateMap(marketIssue1);

        // 3、响应结果
        return BaseRespVo.ok(marketIssue);
    }

    public BaseRespVo delete(HttpServletRequest request) throws IOException {
        // 1、请求参数
        String jsonStr = request.getReader().readLine();
//        MarketIssue marketIssue = JacksonUtil.read(jsonStr, MarketIssue.class);
        Map deleteInfoMap = JacksonUtil.read(jsonStr, Map.class);

        // 2、Service处理业务
//        issueService.delete(marketIssue);
        issueService.deleteMap(deleteInfoMap);


        // 3、响应结果
        return BaseRespVo.ok(null);
    }
}
